
package org.apache.solr.schema;

import java.io.IOException;
import java.util.Map;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.queries.function.docvalues.DocTermsIndexDocValues;
import org.apache.lucene.queries.function.valuesource.FieldCacheSource;

public class StrFieldSource extends FieldCacheSource {

    public StrFieldSource(String field) {
        super(field);
    }

    @Override
    public String description() {
        return "str(" + field + ')';
    }

    @Override
    public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {
        return new DocTermsIndexDocValues(this, readerContext, field) {
            @Override
            protected String toTerm(String readableValue) {
                return readableValue;
            }

            @Override
            public int ordVal(int doc) {
                return termsIndex.getOrd(doc);
            }

            @Override
            public int numOrd() {
                return termsIndex.numOrd();
            }

            @Override
            public Object objectVal(int doc) {
                return strVal(doc);
            }

            @Override
            public String toString(int doc) {
                return description() + '=' + strVal(doc);
            }
        };
    }

    @Override
    public boolean equals(Object o) {
        return o instanceof StrFieldSource
                && super.equals(o);
    }
    private static int hcode = SortableFloatFieldSource.class.hashCode();

    @Override
    public int hashCode() {
        return hcode + super.hashCode();
    }
;
}
